package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.MobNotifyService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.MobNotify;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @version 1.0
 * @Description: 通知管理
 * @author: Rock
 * @time: 2020/9/11 17:34
 */
@Controller
@RequestMapping("notify")
public class MobNotifyController extends BaseController {

    @Resource
    private MobNotifyService mobNotifyService;

    /**
     * @param page
     * @param type
     * @time: 2020/12/26 10:53
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page, Integer type) {
        ModelAndView mv = new ModelAndView();
        Page<MobNotify> pages = mobNotifyService.list(page, type, this.getSessionUser());
        mv.addObject("notifys", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("type", type);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("notify/notify_list");
        return mv;
    }

    /**
     * 添加
     *
     * @time: 2020/12/26 10:56
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("notify/notify_add");
        return mv;
    }

    /**
     * @param mobNotify
     * @param request
     * @time: 2020/10/22 18:30
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(MobNotify mobNotify, MultipartHttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
        mobNotify.setCreator(u.getId());
        mobNotifyService.add(mobNotify, request.getFile("includeQRcodeImageFile"),
                request.getFile("bottomBtnImageFile"), this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑页面
     *
     * @param id
     * @time: 2020/10/22 22:16
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("notify", mobNotifyService.findById(id));
        mv.setViewName("notify/notify_edit");
        return mv;
    }

    /**
     * @param mobNotify
     * @param request
     * @time: 2020/10/22 22:42
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(MobNotify mobNotify, MultipartHttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        mobNotifyService.update(mobNotify, request.getFile("includeQRcodeImageFile"),
                request.getFile("bottomBtnImageFile"), this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除
     *
     * @param id
     * @time: 2020/12/26 11:09
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return mobNotifyService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @param title
     * @time: 2020/12/26 18:52
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("listAllNotify")
    public Response<Object> toNotifySelect(String title) {
        return Response.successResponse(mobNotifyService.listAll(title));
    }

}
